//
//  WorkflowListView.m
//  RSW.IOS
//
//  Created by zhaiqiuqiu on 2017/8/24.
//  Copyright © 2017年 com.xtion.crm. All rights reserved.
//

#import "WorkflowListView.h"

@interface WorkflowListView ()
{
    UIImageView *_headImageView;
    UILabel *_titleLabel;
}
@end

@implementation WorkflowListView

@synthesize szTag;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.userInteractionEnabled = NO;
        
        self.layer.cornerRadius = 5;
        self.layer.masksToBounds = YES;
        
        self.backgroundColor = [UIColor redColor];
        
        _headImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 45, 45)];
        _headImageView.layer.cornerRadius = 5;
        _headImageView.clipsToBounds = YES;
        [self addSubview:_headImageView];
        
        [self addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGestureRecognizer:)]];
        
        CGSize s = [NSString charSizeWithSystemFontSize:16];
        _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, s.width*4, s.height)];
        _titleLabel.backgroundColor = [UIColor clearColor];
        _titleLabel.font = [UIFont systemFontOfSize:16];
        _titleLabel.textColor = [UIColor whiteColor];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_titleLabel];
        
        [self initVariable];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame userInteractionEnabled:(BOOL)userInteractionEnabled
{
    self = [self initWithFrame:frame];
    
    self.userInteractionEnabled = userInteractionEnabled;
    
    return self;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    _headImageView.left = 10;
    _headImageView.centerY = self.centerY;
    
    CGFloat leftWidth = self.width - _headImageView.right;
    _titleLabel.centerX = _headImageView.right + leftWidth/2.0f;
    _titleLabel.centerY = _headImageView.centerY;
}

- (void)initVariable
{
    [self addMainThreadObserverWithDictionarySelector:@{XLGHeadImageDownloadQueueHeadImageDownloadResultNotification:@"observerHeadImageDownloadQueueHeadImageDownloadResultNotification:"}];
}

-(void)setHeadImgUrl:(NSString *)headImgUrl
{
    UIImage *headImage = nil;
    if(headImgUrl.length >0)
    {
        if([headImgUrl rangeOfString:ServiceDefaultHeadImageTag].location == NSNotFound)
        {
            XLGHeadImageCache *cache = [XLGHeadImageDownloadQueue checkOrStartDownloadHeadImageWhenAddHeadImageURL:headImgUrl forKey:XLGHeadImageDownloadQueueSysIcon];
            if(cache != nil)
                headImage = cache;
            else
                headImage = [UIImage imageNamed:@"Icon"];
        }
        else
        {
            NSString *h = [headImgUrl substringFromIndex:[headImgUrl rangeOfString:ServiceDefaultHeadImageTag].length];
            headImage = [UIImage imageNamed:h];
        }
    }
    else
        headImage = [UIImage imageNamed:@"Icon"];
    
    headImage = [UIImage scaleImage:headImage size:CGSizeMake(100.0,100.0)];

    _headImageView.image = headImage;
}

-(void)setTitle:(NSString *)title
{
    _titleLabel.text = title;
}

-(void)tapGestureRecognizer:(UITapGestureRecognizer*)tapGestureRecognizer
{
    switch(tapGestureRecognizer.state)
    {
        case UIGestureRecognizerStateEnded:
        {
            if([_delegate respondsToSelector:@selector(clickWorkflowListView:)])
                [_delegate clickWorkflowListView:self];
            break;
        }
        default:
            break;
    }
}

#pragma mark --
#pragma mark -- refresh headImage
-(void)observerHeadImageDownloadQueueHeadImageDownloadResultNotification:(NSNotification*)noti
{
    NSString *path =[noti.userInfo objectForKey:XLGHeadImageDownloadQueueHeadImageDownloadResultPathKey];
    NSString *URL =[noti.userInfo objectForKey:XLGHeadImageDownloadQueueHeadImageDownloadResultURLKey];
    
    XLGHeadImageCache *cache = [XLGHeadImageDownloadQueue setCacheWithHeadImageURL:URL andHeadImagePath:path forKey:XLGHeadImageDownloadQueueSysIcon];
    
    UIImage *headImage = (UIImage*)cache;
    headImage = [UIImage scaleImage:headImage size:CGSizeMake(45.0,45.0)];
    
    _headImageView.image = headImage;
}
@end
